package com.smt.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.internet.MimeMessage;
import java.util.Map;


/**
 * 发送邮件
 *
 * @author zhangyi
 * @date 2018/10/16 15:15
 */
@Component("emailUtil")
@ConfigurationProperties(prefix = "spring.freemarker")
@Slf4j
public class EmailUtil {

    private String mailPath;

    public String getMailPath() {
        return mailPath;
    }

    public void setMailPath(String mailPath) {
        this.mailPath = mailPath;
    }

    /**
     * 发件邮箱
     */
    @Value("${spring.mail.username}")
    private String mailFrom;

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private FreeMarkerConfigurer configurer;

    /**
     * 发送简单邮件
     *
     * @param receiver 收件人地址
     * @param subject  主题
     * @param content  内容
     */
    @Async
    public void sendSimpleMail(String receiver, String subject, String content) {
        //添加html邮件模板
        sendSimpleMail(mailFrom, receiver, subject, content);
    }

    @Async
    public void sendMail(String receiver, String subject,Map<String,Object> map,String fileName) {
        //添加html邮件模板
        sendMessageMail(mailFrom, receiver, subject,map,fileName);
    }

    /**
     * 发送简单邮件
     *
     * @param mailFrom 发件人地址
     * @param receiver 收件人地址
     * @param subject  主题
     * @param content  内容
     */
    private void sendSimpleMail(String mailFrom, String receiver, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailFrom);
        message.setTo(receiver);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 发送模板邮件
     *
     * @param mailFrom
     * @param to
     * @param subject
     * @param map
     * @param fileName
     */
    private void sendMessageMail(String mailFrom,String to, String subject,Map<String,Object> map,String fileName){
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try{
            MimeMessageHelper mh=new MimeMessageHelper(mimeMailMessage,true);
            mh.setFrom(mailFrom);
            mh.setTo(to);
            mh.setSubject(subject);
            freemarker.template.Template template = configurer.getConfiguration().getTemplate(mailPath+fileName, "UTF-8");
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            mh.setText(text,true);
            mailSender.send(mimeMailMessage);
        }catch (Exception e){
            e.printStackTrace();
            log.error("模板邮件发送失败");
        }
    }
}
